class binarysearchTree {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode head;

    /*public TreeNode creat() {
        TreeNode a = new TreeNode(5);
        TreeNode b = new TreeNode(3);
        TreeNode c = new TreeNode(7);
        TreeNode d = new TreeNode(1);
        TreeNode e = new TreeNode(4);
        TreeNode f = new TreeNode(6);
        TreeNode z = new TreeNode(8);
        a.left = b;
        a.right = c;
        b.left = d;
        b.right = e;
        c.left = f;
        c.right = z;
        return a;
    }

     */

    public TreeNode search(int cur) {
        //TreeNode head = creat();
        TreeNode c=this.head;
        while (c != null) {
            if (c.val > cur) {
               c = c.left;

            } else if (c.val < cur) {
               c = c.right;
            } else {
                return c;
            }

        }
        return null;
    }
    public void offer(int cur)
    {
        TreeNode m=new TreeNode(cur);

        if(this.head==null) {


            this.head=m;
            return;
        }
        TreeNode c=this.head;
        TreeNode parent=null;

        //TreeNode head = creat();
        while(c!=null)
        {
            parent=c;
            if (c.val > cur) {
               c = c.left;


            } else if(c.val < cur) {
                c = c.right;
            } else {
                System.out.println("有相同的数据无法插入");


            }

        }
        if(cur>parent.val)
        {
            //m=parent.right;
            parent.right=m;
            return;

        }
        else
        {
            parent.left=m;
            return;
        }




    }
    public void remove(int cur)
    {
        TreeNode c=this.head;
        TreeNode parent1=null;
        while (c != null) {
            if (c.val > cur) {
                parent1=c;
                c = c.left;

            } else if (c.val < cur) {
                parent1=c;
                c = c.right;
            } else {
                if(c.left==null)
                {
                    if(c==this.head)
                    {
                        head=c.right;
                    }
                    else if(parent1.left==c)
                    {
                        parent1.left=c.right;

                    }
                    else
                    {
                        parent1.right=c.right;
                    }
                }
                else if(c.right==null)
                {
                    if(c==this.head)
                    {
                        head=c.left;
                    }
                    else if(parent1.left==c)
                    {
                        parent1.left=c.left;
                    }
                    else
                    {
                        parent1.right=c.left;
                    }
                }
                else {
                       TreeNode target=c.left;
                       TreeNode hou=c;
                    while(target.right!=null)
                    {
                        hou=c;
                        target=target.right;
                    }
                    c.val=target.val;
                    if(hou.right==target) {
                        hou.right = target.left;
                    }
                    else
                    {
                        hou.left=target.left;

                    }


                }

            }

        }



    }

}




public class text11 {
    public static void main(String[] args) {
        binarysearchTree a=new binarysearchTree();
        //

        //System.out.println(d.val);
        a.offer(6);
        a.offer(3);
        a.offer(8);
        a.offer(1);
        a.offer(14);
        a.offer(7);
        a.offer(13);
        //binarysearchTree.TreeNode d=a.search(4);
      a.remove(8);

        System.out.println(1);



    }
}



